package com.wjp.banquet.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjp.banquet.common.enums.BanquetIsSealEnum;
import com.wjp.banquet.common.enums.DelFlagEnum;
import com.wjp.banquet.common.enums.SortTypeEnum;
import com.wjp.banquet.common.enums.YesOrNoEnum;
import com.wjp.banquet.common.util.ParamCheckUtil;
import com.wjp.banquet.common.util.StringUtil;
import com.wjp.banquet.common.util.UserUtil;
import com.wjp.banquet.dto.BanquetAddDTO;
import com.wjp.banquet.dto.BanquetPageQueryDTO;
import com.wjp.banquet.dto.BanquetSealOptDTO;
import com.wjp.banquet.entity.Banquet;
import com.wjp.banquet.mapper.BanquetMapper;
import com.wjp.banquet.mapper.GiftMoneyMapper;
import com.wjp.banquet.service.BanquetService;
import com.wjp.banquet.vo.BanquetVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 王君平
 * @Date 2024/4/23 16:54
 * @Description
 */
@Service
public class BanquetServiceImpl implements BanquetService {

    @Autowired
    private BanquetMapper banquetMapper;

    @Autowired
    private GiftMoneyMapper giftMoneyMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BanquetAddDTO dto) {
        String name = dto.getName();
        List<Banquet> list = banquetMapper.getByName(name);
        ParamCheckUtil.collectionNotEmpty(list,"该名称已经存在");


        Banquet cart = new Banquet();
        BeanUtils.copyProperties(dto,cart);
        cart.setUserId(UserUtil.getCurrentUserId());
        cart.setIsSeal(BanquetIsSealEnum.NO.isCode());
        cart.fillUpToSave();
        banquetMapper.insert(cart);
    }

    @Override
    public PageInfo<BanquetVO> pageList(BanquetPageQueryDTO dto) {
        if(StringUtil.isEmpty(dto.getSortName())){
            dto.setSortName("id");
            dto.setSortType(SortTypeEnum.DESC.getCode());
        }
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        PageHelper.orderBy(dto.getOrderBy());
        dto.setUserId(UserUtil.getCurrentUserId());

        List<Banquet> list = banquetMapper.getPageList(dto);
        PageInfo<Banquet> pageInfo = new PageInfo<>(list);
        PageInfo<BanquetVO> voPageInfo = new PageInfo<>();
        if(CollectionUtils.isEmpty(list)){
            return voPageInfo;
        }
        BeanUtils.copyProperties(pageInfo,voPageInfo);
        voPageInfo.setList(this.poToVo(list));
        return voPageInfo;
    }

    @Override
    public BanquetVO getById(Integer id) {
        return this.poToVo(banquetMapper.selectById(id));
    }

    @Override
    public BanquetVO getByIdWithSummary(Integer id) {
        BanquetVO vo = this.poToVo(banquetMapper.selectById(id));
        BanquetVO summary = giftMoneyMapper.getSummaryByBanquetId(id);
        vo.setTotalGuest(summary.getTotalGuest());
        vo.setTotalMoney(summary.getTotalMoney());
        return vo;
    }

    @Override
    public List<BanquetVO> allList(BanquetPageQueryDTO dto) {
        if(StringUtil.isEmpty(dto.getSortName())){
            dto.setSortName("id");
            dto.setSortType(SortTypeEnum.DESC.getCode());
        }
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        PageHelper.orderBy(dto.getOrderBy());

        List<Banquet> list = banquetMapper.getPageList(dto);
        return this.poToVo(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void seal(BanquetSealOptDTO dto) {
        Banquet banquet = banquetMapper.selectById(dto.getId());
        ParamCheckUtil.objectNull(banquet,"酒席不存在");
        ParamCheckUtil.notTrue(DelFlagEnum.USE.isCode() == banquet.getDelFlag(),"该酒席已经删除");

        ParamCheckUtil.notTrue(BanquetIsSealEnum.NO.isCode() == banquet.getIsSeal(), "该酒席已经是封存状态" );

        Banquet update = new Banquet();
        update.setId(banquet.getId());
        update.setIsSeal(BanquetIsSealEnum.YES.isCode());
        banquetMapper.updateById(update);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unSeal(BanquetSealOptDTO dto) {
        Banquet banquet = banquetMapper.selectById(dto.getId());
        ParamCheckUtil.objectNull(banquet,"酒席不存在");
        ParamCheckUtil.notTrue(DelFlagEnum.USE.isCode() == banquet.getDelFlag(),"该酒席已经删除");

        ParamCheckUtil.notTrue(BanquetIsSealEnum.YES.isCode() == banquet.getIsSeal(), "该酒席已经是未封存状态" );

        //ParamCheckUtil.isTrue(true, "该酒席是封存状态，不允许操作" );

        Banquet update = new Banquet();
        update.setId(banquet.getId());
        update.setIsSeal(BanquetIsSealEnum.NO.isCode());
        banquetMapper.updateById(update);
    }

    private List<BanquetVO> poToVo(List<Banquet> list) {
        return list.stream().map(v -> poToVo(v)).collect(Collectors.toList());
    }

    private BanquetVO poToVo(Banquet po) {
        BanquetVO vo = new BanquetVO();
        BeanUtils.copyProperties(po,vo);
        vo.setIsSealName(BanquetIsSealEnum.getNameByCode(po.getIsSeal()));
        return vo;
    }
}
